package com.cloud.tsp.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.api.domain.tsp.QueryVehicleOwner;
import com.cloud.api.domain.tsp.VehicleOwner;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.mapper.VehicleOwnerMapper;
import com.cloud.tsp.service.IVehicleOwnerService;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2023-11-27
 */
@Service("vehicleOwnerService")
public class VehicleOwnerServiceImpl extends ServiceImpl<VehicleOwnerMapper, VehicleOwner> implements IVehicleOwnerService {

    @Resource
    private VehicleOwnerMapper vehicleOwnerMapper;
    @Resource
    private MessageSource messageSource;

    @Override
    public TableDataInfo<VehicleOwner> selectOwnerList(QueryVehicleOwner para) {
        Page<VehicleOwner>page = new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
        IPage<VehicleOwner> data = vehicleOwnerMapper.selectOwnerList(page, para);
        return TableDataInfo.build(data);
    }

    @Override
    public void insertOrUpdate(VehicleOwner para) {
        //查询
        if (StringUtil.isBlank(para.getId())){
            para.setId(IdUtil.getSnowflakeNextIdStr());
            vehicleOwnerMapper.insert(para);
        }else {
            para.setVin(null);
            vehicleOwnerMapper.updateById(para);
        }
    }

    @Override
    public void exportOwnerList(QueryVehicleOwner para, HttpServletResponse response)throws IOException  {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.OwnerName";
        String message = messageSource.getMessage(fileName, null, fileName, locale);
        Map<String, String> map = translateFields(locale);
        ExcelWriter bigWriter = ExcelUtil.getBigWriter();
        List<VehicleOwner> vehicleOwners = vehicleOwnerMapper.selectAllOwnerList(para);
        bigWriter.setColumnWidth(0, 20);
        bigWriter.setColumnWidth(1, 20);
        bigWriter.setColumnWidth(2, 20);
        bigWriter.setColumnWidth(3, 20);
        bigWriter.setColumnWidth(4, 20);
        bigWriter.addHeaderAlias("vin", map.get("vin"));
        bigWriter.addHeaderAlias("phoneNumber", map.get("ownerPhoneNumber"));
        bigWriter.addHeaderAlias("carOwner",map.get("carOwner"));
        bigWriter.addHeaderAlias("licenseNumber", map.get("licenseNumber"));
        bigWriter.addHeaderAlias("createTime", map.get("createTime"));
        bigWriter.setOnlyAlias(true);
        CellStyle headCellStyle = bigWriter.getHeadCellStyle();
        headCellStyle.setFillBackgroundColor((short) 22);
        bigWriter.write(vehicleOwners);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(message+".xlsx", "UTF8"));
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        try {
            Workbook workbook = bigWriter.getWorkbook();
            ServletOutputStream out=response.getOutputStream();
            workbook.write(out);
            workbook.close();
            out.close();
            bigWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Map<String,String> translateFields(Locale locale){
        Map<String,String> map = new HashMap<>();
        String vin = "table.field.vin";
        String ownerPhoneNumber = "table.field.ownerPhoneNumber";
        String carOwner = "table.field.carOwner";
        String licenseNumber = "table.field.licenseNumber";
        String createTime = "table.field.createTime";

        map.put("vin",messageSource.getMessage(vin, null, vin, locale));
        map.put("ownerPhoneNumber",messageSource.getMessage(ownerPhoneNumber, null, ownerPhoneNumber, locale));
        map.put("carOwner",messageSource.getMessage(carOwner, null, carOwner, locale));
        map.put("licenseNumber",messageSource.getMessage(licenseNumber, null, licenseNumber, locale));
        map.put("createTime",messageSource.getMessage(createTime, null, createTime, locale));
        return map;
    }
}
